package com.example.freshmenclient;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.example.freshmenclient.util.GainDataUtil;
import com.example.freshmenclient.util.UrlUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class RegisterActivity extends AppCompatActivity {

    private static final String TAG = "RegisterActivity";
    private Spinner sPSchoolDept;
    private Spinner sPMajor;
    private ArrayList<String> schoolDepts;
    private ArrayList<String> majors;
    private EditText etUsername;
    private EditText etEmail;
    private EditText etPhone;
    private EditText etPassword;
    private EditText etRePassword;
    private String majorStr;
    private String deptStr;
    private ArrayAdapter<String> schoolDeptsArrayAdapter;
    private ArrayAdapter<String> majorsArrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void dropDownBox() {
        schoolDepts = new ArrayList<>();
        schoolDepts.add("信息学院");
        schoolDepts.add("网络技术学院");
        schoolDepts.add("人文学院");
        schoolDeptsArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, schoolDepts);
        sPSchoolDept.setAdapter(schoolDeptsArrayAdapter);
        majors = new ArrayList<>();
        majors.add("移动应用开发");
        majors.add("软件技术");
        majors.add("计算机网络技术");
        majorsArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, majors);
        sPMajor.setAdapter(majorsArrayAdapter);
    }

    private void initData() {
        String username = etUsername.getText().toString();
        String email = etEmail.getText().toString();
        String phone = etPhone.getText().toString();
        String password = etPassword.getText().toString();
        String rePassword = etRePassword.getText().toString();
        deptStr = sPSchoolDept.getSelectedItem().toString();
        majorStr = sPMajor.getSelectedItem().toString();
        Log.e(TAG, "initData: " + deptStr + majorStr);
        if (!password.equals(rePassword)) {
            Toast.makeText(this, "密码与确认密码不一致，请重新输入！", Toast.LENGTH_SHORT).show();
            return;
        }
        String url = UrlUtil.basePath + "freshmenserver/user/register";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name", username);
            jsonObject.put("password", password);
            jsonObject.put("email", email);
            jsonObject.put("telephone", phone);
            jsonObject.put("department", deptStr);
            jsonObject.put("major", majorStr);
            jsonObject.put("role", "学生");
            GainDataUtil.gainDataForPost(this, url, jsonObject
                    , new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                            try {
                                Log.e(TAG, "onResponse: " + jsonObject + jsonObject.getBoolean("success"));

                                if (jsonObject.getBoolean("success")) {
                                    //注册成功，跳转到登录页面
                                    startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                                } else {
                                    Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, null);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        sPSchoolDept = findViewById(R.id.sp_school_department);
        sPMajor = findViewById(R.id.sp_major);

        etUsername = findViewById(R.id.et_username);
        etEmail = findViewById(R.id.et_email);
        etPhone = findViewById(R.id.et_phone);
        etPassword = findViewById(R.id.et_password);
        etRePassword = findViewById(R.id.et_re_password);

        dropDownBox();
    }

    public void regist(View view) {
        initData();
    }

    public void back(View view) {
        finish();
    }
}
